package util;

import dataset.Dataset;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CsvUtils {

    public static List<List<String>> csvToList(String filePath, String split) {
        String line = "";
        List<List<String>> result = new ArrayList<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            while ((line = bufferedReader.readLine()) != null) {
                ArrayList<String> lineResult = new ArrayList<>();
                for (String record : line.split(split)) {
                    lineResult.add(record);
                }
                result.add(lineResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int max = result.stream().mapToInt(c -> c.size()).max().getAsInt();
        for (int i = 0; i < result.size(); i++) {
            while (result.get(i).size() < max) {
                result.get(i).add("");
            }
        }
        return result;
    }

    public static List<Map<String, String>> csvToMap(String filePath, String split) {
        String line = "";
        List<Map<String, String>> result = new ArrayList<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            while ((line = bufferedReader.readLine()) != null) {
                Map<String, String> lineResult = new LinkedHashMap<>();
                String[] strings = line.split(split);
                for (int i = 0; i < strings.length; i++) {
                    lineResult.put("c" + i, strings[i]);
                }
                result.add(lineResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int max = result.stream().mapToInt(map -> map.size()).max().getAsInt();
        for (int i = 0; i < result.size(); i++) {
            while (result.get(i).size() < max) {
                result.get(i).put("c" + result.get(i).size(), "");
            }
        }
        return result;
    }


    public static void writeCsv(String filePath, double[][] X, int[] label, String split) {
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
            for (int i = 0; i < X.length; i++) {
                for (double x : X[i])
                    bufferedWriter.write(String.valueOf(x) + ",");
                bufferedWriter.write(String.valueOf(label[i]) + "\n");
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void subCsv(String filePath, String newFilePath, String split, List<String> cols) {
        String line = "";
        List<Map<String, String>> result = new ArrayList<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newFilePath));
            while ((line = bufferedReader.readLine()) != null) {
                String[] strings = line.split(split);
                boolean notFirst = false;
                for (int i = 0; i < strings.length; i++) {
                    String key = "c" + i;
                    if (cols.contains(key)) {
                        if (notFirst)
                            bufferedWriter.write(split);
                        notFirst = true;
                        bufferedWriter.write(strings[i]);
                    }
                }
                bufferedWriter.write("\n");
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
